package com.cluster.gulimall.coupon.service.impl;

import com.cluster.gulimall.common.to.MemberPrice;
import com.cluster.gulimall.common.to.SkuReductionTo;
import com.cluster.gulimall.common.utils.PageUtils;
import com.cluster.gulimall.common.utils.Query;
import com.cluster.gulimall.coupon.entity.SmsMemberPrice;
import com.cluster.gulimall.coupon.entity.SmsSkuFullReduction;
import com.cluster.gulimall.coupon.entity.SmsSkuLadder;
import com.cluster.gulimall.coupon.mapper.SmsSkuFullReductionMapper;
import com.cluster.gulimall.coupon.service.MemberPriceService;
import com.cluster.gulimall.coupon.service.SkuFullReductionService;
import com.cluster.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionMapper, SmsSkuFullReduction> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReduction> page = this.page(
                new Query<SmsSkuFullReduction>().getPage(params),
                new QueryWrapper<SmsSkuFullReduction>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //1、// //5.4）、sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //sms_sku_ladder
        SmsSkuLadder skuLadderEntity = new SmsSkuLadder();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        skuLadderEntity.setDiscount(reductionTo.getDiscount());

        if (reductionTo.getCountStatus() == 0){
            skuLadderEntity.setAddOther(false);
        }else if(reductionTo.getCountStatus() == 1){
            skuLadderEntity.setAddOther(true);
        }
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }




        //2、sms_sku_full_reduction
        SmsSkuFullReduction reductionEntity = new SmsSkuFullReduction();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        if(reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }


        //3、sms_member_price
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();

        List<SmsMemberPrice> collect = memberPrice.stream().map(item -> {
            SmsMemberPrice priceEntity = new SmsMemberPrice();
            priceEntity.setSkuId(reductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(true);
            return priceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());

        memberPriceService.saveBatch(collect);
    }

}